import axios from 'axios'
import { SESSION } from '@/utils/storage'

const request = axios.create({
  baseURL: import.meta.env.VITE_API_BASE_URL,
  timeout: 60 * 1000,
  headers: {
    'Content-Type': 'application/json',
  },
})

request.interceptors.request.use((config) => {
  const access_token = SESSION.get('access_token')
  config.headers['Authorization'] = access_token ? `Bearer ${access_token}` : ''
  return config
})

request.interceptors.response.use(
  (response) => {
    const responseData = response.data
    if (responseData.status === 'error') {
      return Promise.reject(responseData)
    }
    return responseData
  },
  (error) => {
    return Promise.reject(error)
  },
)

export default request
